package net.sf.rose.boot.bean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import lombok.Data;
import net.sf.rose.boot.BootStrap;
import net.sf.rose.util.RoseRuntimeException;

/**
 * 类说明：程序主配置文件类
 * 
 * @author fengjian
 * @since 2019/03/02
 */
@Data
@Root
public class BootStrapBean {

	/** 后台服务端口号 */
	@Attribute(required = false)
	private int port = 9980;

	/** 默认数据库名 */
	@Element(required = false)
	private String defaultDatabaseName = null;

	/** 是否更新数据库表结构 */
	@Element(required = false)
	private boolean updateDatabase = false;

	/** 是否更新数据库表删除字段 */
	@Element(required = false)
	private boolean updateDeleteField = false;

	/** Web服务器配置 */
	@Element(required = false)
	private WebServerBean webServerBean = new WebServerBean();

	/** 需要启动的定时任务 */
	@ElementList(required = false)
	private List<RunTaskBean> runTaskList = new ArrayList<>();

	/** 数据库字段定义元数据 */
	@ElementList(required = false)
	private List<DataTypeList> dataTypeList = new ArrayList<>();

	/** Web服务器配置 */
	private Map<String, DatabaseBean> databaseMap = new HashMap<>();

	private File file = null;

	public void initialDatabaseBean(DatabaseBean databaseBean) {
		String type = databaseBean.getType();
		for (DataTypeList dataType : dataTypeList) {
			if (dataType.getName().equals(type)) {
				databaseBean.initialFieldList(dataType.getFieldList());
			}
		}
		databaseMap.put(databaseBean.getFileName(), databaseBean);
	}

	/**
	 * 功能：根据数据库名称获取数据库的定义类
	 * 
	 * @param databaseName - 数据库名称
	 * @return 数据库的定义类
	 */
	public DatabaseBean getDatabaseBean(String databaseName) {
		return databaseMap.get(databaseName);
	}

	/**
	 * 功能：设置配置文件存储路径
	 * 
	 * @param file 配置文件存储路径
	 */
	public void setFile(File file) {
		this.file = file;
	}

	/**
	 * 功能：在指定的配置文件目录下加载配置信息
	 */
	public void load() {
		if (file == null) {
			file = BootStrap.getResourceFile("sys.xml");
		}
		if (file.exists()) {
			try {
				Serializer serializer = new Persister();
				BootStrapBean bean = serializer.read(BootStrapBean.class, file);
				this.port = bean.port;
				this.updateDatabase = bean.updateDatabase;
				this.updateDeleteField = bean.updateDeleteField;
				this.defaultDatabaseName = bean.defaultDatabaseName;
				this.runTaskList = bean.runTaskList;
				this.dataTypeList = bean.dataTypeList;
				this.webServerBean = bean.webServerBean;
			} catch (Exception e) {
				throw new RoseRuntimeException("读取配置文件：" + file.getAbsolutePath() + "，时发生错误！" + e.getMessage(), e);
			}
		} else {
			this.save();
		}
	}

	/**
	 * 功能：保存配置文件信息
	 */
	public void save() {
		try {
			Serializer serializer = new Persister();
			serializer.write(this, file);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RoseRuntimeException("保存配置文件：" + file.getAbsolutePath() + "，时发生错误:" + e.getMessage(), e);
		}
	}

}
